package com.gitee.T7;

import java.util.Scanner;

public class t7 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] strs = new String[0];
        System.out.println("输入字符串数组（输入0结束）：");
        while (true) {
            String str = sc.next();
            if (str.equals("0")) {
                break;
            }
            strs = creatStrArr(strs, str);
        }
        FindAffixes(strs);
    }

    //创建字符串数组
    public static String[] creatStrArr(String[] strs, String num) {
        String[] newstrs = new String[strs.length + 1];
        for (int i = 0; i < strs.length; i++) {
            newstrs[i] = strs[i];
        }
        newstrs[strs.length] = num;
        return newstrs;
    }

    public static void FindAffixes(String[] strs) {
        //找出最短字符串的长度
        int minLen = strs[0].length();
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].length() < minLen) {
                minLen = strs[i].length();
            }
        }
        //找出公共前缀
        String common = strs[0].substring(0, 1);
        boolean flag = true;
        for (int i = 0; i < minLen; i++) {
            for (int j = 0; j < strs.length; j++) {
                if (!common.equals(strs[j].substring(0, 1 + i))) {
                    flag = false;
                    if (i == 0) {
                        common = "";
                    } else {
                        common = strs[0].substring(0, 1 + i - 1);
                    }
                    break;
                }
            }
            if (!flag || i == minLen - 1) {
                break;
            }
            common = strs[0].substring(0, 1 + i + 1);
        }
        System.out.println("\"" + common + "\"");
    }
}
